<?php

/**
 * BlogController is the default controller for this application
 */
class BlogController extends ControllerBase {
	
	/**
	 * Admin Blog Data Service Object
	 * @var AdminBlogDataService object
	 */
	private $adminBlogDataService;
	
	/**
	 * BlogController Constructor
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set AdminBlog name
		$this->setPageName ( 'admin_blog' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if ($authentication->isAdministrator () == false) {
			
			// Go to login AdminBlog
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of AdminBlog category
		$this->smarty->assign ( 'statusList', AdminBlogEntity::getStatusList () );
		
		// New AdminBlog Data Service
		$this->adminBlogDataService = new AdminBlogDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing AdminBlog of the site. Return list of all AdminBlog category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View AdminBlog_category list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of AdminBlog_category
	 * landing AdminBlog of the site. Return list of all AdminBlog_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		
		$arrElement = array ();
		$arrElement ['status'] = AdminBlogEntity::ACTIVE;
		
		$numRecordPerPage = ITEM_PER_PAGE;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->adminBlogDataService->getTotals ( $arrElement );
		
		$itemList = $this->adminBlogDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Blog/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'adminBlogEntities', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Blog/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add action is trigger when admin want to add the AdminBlog_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {
		
		$isError = false;
		$msg = "";
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->validateActionKey ()) 

		{
			
			// Get name field
			$title = $this->getPost ( 'title', "" );
			$title = trim ( $title );
			
			// get content
			$body = $this->getPost ( 'body', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$body = stripslashes ( $body );
			}
			
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				
				// Ignore this case
				$resultMessage = 'Upload banner fail.';
			}
			
			$body = trim ( $body );
			
			// head title
			$metaTitle = $this->getPost ( 'metaTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Get status field
			$status = $this->getPost ( 'status', 0 );
			
			// check
			if (! empty ( $title ) && ! empty ( $body ) && is_numeric ( $status )) {
				$status = intval ( $status );
				try {
					
					// Init Authentication class
					$authentication = new Authentication ();
					// Save data into database
					$adminBlogEntity = $this->adminBlogDataService->beforeCreate ();
					$adminBlogEntity->title = $title;
					$adminBlogEntity->body = $body;
					$adminBlogEntity->picture = $picture;
					$adminBlogEntity->metaTitle = $metaTitle;
					$adminBlogEntity->ownerId = $authentication->getCurrentUserId ();
					$adminBlogEntity->status = $status;
					$adminBlogEntity->metaDescription = $metaDescription;
					$adminBlogEntity->metaKeywords = $metaKeywords;
					$adminBlogEntity->commentCount = 0;
					$adminBlogEntity->viewCount = 0;
					
					$adminBlogId = $this->adminBlogDataService->create ( $adminBlogEntity );
					
					// Redirect to edit AdminBlog
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Blog/edit/id/' . $adminBlogId . '/justCreated/1/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					
					echo $ex->getMessage ();
				
				}
			} else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
		}
		
		// Create new token key
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'title', isset ( $title ) ? $title : "" );
		$this->smarty->assign ( 'body', isset ( $body ) ? $body : "" );
		$this->smarty->assign ( 'metaTitle', isset ( $metaTitle ) ? $metaTitle : "" );
		$this->smarty->assign ( 'status', isset ( $status ) ? $status : 0 );
		$this->smarty->assign ( 'metaDescription', isset ( $metaDescription ) ? $metaDescription : "" );
		$this->smarty->assign ( 'metaKeywords', isset ( $metaKeywords ) ? $metaKeywords : "" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Blog/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit action is trigger when admin want to edit the AdminBlog_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		// Error flag
		$isError = false;
		$msg = "";
		
		// Get id of AdminBlog
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of AdminBlog does not exist, go on to view list AdminBlog
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->listAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// just created
		$justCreated = $this->getQuery ( 'justCreated' );
		if (! empty ( $justCreated ) && $justCreated == "1") {
			$msg = "The item has been created successfully";
		}
		
		// Get data
		$adminBlogEntity = $this->adminBlogDataService->getById ( $id );
		
		// check data
		if (empty ( $adminBlogEntity )) {
			$this->listAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->validateActionKey ()) {
			
			// Get name field
			$title = $this->getPost ( 'title', "" );
			$title = trim ( $title );
			
			// get content
			$body = $this->getPost ( 'body', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$body = stripslashes ( $body );
			}
			
			$body = trim ( $body );
			
			// Upload picture
			$picture = "";
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				
				// Ignore this case
				$resultMessage = 'Upload banner fail.';
			}
			
			if (empty ( $picture )) {
				$picture = $adminBlogEntity->picture;
			}
			
			// head title
			$metaTitle = $this->getPost ( 'metaTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Get status field
			$status = $this->getPost ( 'status', 0 );
			
			$adminBlogEntity->title = $title;
			$adminBlogEntity->body = $body;
			$adminBlogEntity->picture = $picture;
			$adminBlogEntity->metaTitle = $metaTitle;
			$adminBlogEntity->status = $status;
			$adminBlogEntity->metaDescription = $metaDescription;
			$adminBlogEntity->metaKeywords = $metaKeywords;
			
			// check
			if (! empty ( $title ) && ! empty ( $body ) && is_numeric ( $status )) {
				try {
					$res = $this->adminBlogDataService->update ( $adminBlogEntity );
					
					$isError = false;
					
					// Show a message
					$msg = 'The record has been updated successfully';
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'id', $adminBlogEntity->id );
		$this->smarty->assign ( 'title', $adminBlogEntity->title );
		$this->smarty->assign ( 'body', $adminBlogEntity->body );
		$this->smarty->assign ( 'metaTitle', $adminBlogEntity->metaTitle );
		$this->smarty->assign ( 'status', $adminBlogEntity->status );
		$this->smarty->assign ( 'metaDescription', $adminBlogEntity->metaDescription );
		$this->smarty->assign ( 'metaKeywords', $adminBlogEntity->metaKeywords );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Blog/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a AdminBlog_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get Id of AdminBlog
		$id = $this->getPost ( 'idValue', 0 );
		
		if ($this->validateActionKey () && ! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			// Delete AdminBlog
			$res = $this->adminBlogDataService->del ( $id );
		}
		$this->listAction ();
	}

}
